package com.tanhua.server.controller;


import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import com.tanhua.server.service.TanhuaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;
    @Autowired
    private TanhuaService tanhuaService;

    /**
     * 发布视频
     * 接口路径：POST
     * 请求参数：
     * videoThumbnail：封面图
     * videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/{id}/like")
    public ResponseEntity VideoLikes(@PathVariable String id) {
        Integer LikeCount = videosService.VideoLikes(id);
        return ResponseEntity.ok(LikeCount);
    }

    /**
     * 视频取消点赞
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity VideodisLikes(@PathVariable String id) {
        Integer LikeCount = videosService.VideodisLikes(id);
        return ResponseEntity.ok(LikeCount);
    }

    /**
     * @param id    视频ID
     * @param
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity VideoComments(@PathVariable String id, @RequestBody Map map) {
        String comment = (String) map.get("comment");
        videosService.saveComments(id, comment);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/{id}/comments")
    public ResponseEntity QueryComments(@PathVariable String id,
                                        @RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = videosService.queryVideoComment(id,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频评论点赞
     * @param id 评论ID
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity findLike(@PathVariable String id){
        Integer count = videosService.Commentlike(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频评论取消点赞
     * @param id 评论ID
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity finddisLike(@PathVariable String id){
        Integer count = videosService.CommentDislike(id);
        return ResponseEntity.ok(count);

    }

    /**
     * 关注小视频用户
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable Long uid){
        tanhuaService.likeUser(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable Long uid){
        tanhuaService.notLikeUser(uid);
        return ResponseEntity.ok(null);
    }
}